package com.example.permission.myjbm.net;

import android.os.Bundle;
import android.os.Handler;

import com.example.permission.myjbm.config.Config;
import com.jbm.jbmsupplier.util.BusinessEntity;
import com.jbm.jbmsupplier.util.ExProductEntity;
import com.jbm.jbmsupplier.util.IAplipay;
import com.jbm.jbmsupplier.util.ICtrl;
import com.jbm.jbmsupplier.util.IExChangeCtrl;
import com.jbm.jbmsupplier.util.IGeoEntityCtrl;
import com.jbm.jbmsupplier.util.IGeoinfoCtrl;
import com.jbm.jbmsupplier.util.IInformationCtrl;
import com.jbm.jbmsupplier.util.IWeatherCtrl;
import com.jbm.jbmsupplier.util.InfomationEntity;
import com.jbm.jbmsupplier.util.IpointsCtrl;
import com.jbm.jbmsupplier.util.VoucherEntity;
import com.jbm.jbmsupplier.util.abstracts.SendMessageInterfase;
import com.jbm.jbmsupplier.util.enums.ClickTimesType;

/**
 * Created by JBMhaier on 2017/2/24.
 */

public class ReleaseNet extends SendMessageInterfase {

    //标志
    private final  String CMD_CODE = "cmd_code";
    private  Handler handler;
    private Bundle bundle;
    private ICtrl info;
    //bound标志
    private final String RELEASE="发布";
    public static final int RELEASEERROR=0x111;
    private final String HOMELIST="首页列表";
    public static final int HOMELISTERROR=0x112;
    private final String SHOUCANG="收藏";
    public static final int SHOUCANGERROR=0x113;
    private final String LICK="点赞";
    public static final int LICKERROR=0x114;
    private final String SEARCH="搜索";
    public static final int SEARCHERROR=0x115;

    private final String ADDRESS="获取地址";
    public static final int ADDRESSERROR=0x116;
    private final String ADDRESS1="获取地址1";
    public static final int ADDRESSERROR1=0x1161;
    private final String WEATHER="获取天气";
    public static final int WEATHERERROR1=0x117;
    private final String MERCHANT="是否为商家";
    public static final int MERCHANTERROR=0x118;
    private final String JIFENOTAL="总积分";
    public static final int JIFENOTALERROR=0x119;
    private final String JIFENMINGXI="积分明细";
    public static final int JIFENMINGXIERROR=0x120;
    private final String ARROWLIST="可兑换产品列表";
    public static final int ARROWLISTERROR=0x121;

    private final String MECHANTDETAIL="可兑换商品详情";
    public static final int MECHANTDETAILERROR=0x122;

    private final String DUIHUAN="兑换";
    public static final int DUIHUANERROR=0x123;

    private final String DAIJINJUANLIST="代金券列表";
    public static final int DAIJINJUANLISTERROR=0x124;

    private final String DAIJINDETAIL="代金券详情";
    public static final int DAIJINDETAILERROR=0x125;

    private final String SEARCHMECHANT="搜索商家";
    public static final int SEARCHMECHANTERROR=0x126;

    private final String SETLOG="向服务器发送log";

    private final String ALPAY="支付宝支付";
    public static final int ALPAYERROR=0x128;

    public ReleaseNet(Handler handler){
        this.handler=handler;
    }

    @Override
    protected void RunContext() {
        String com = bundle.getString(CMD_CODE);
        //发布
        if (com.equals(RELEASE)) {

            info = gSocket.AddInfomation((InfomationEntity) bundle.getSerializable("obj"));
            handler.sendMessage(handler.obtainMessage(RELEASEERROR, info));

            //首页面信息列表
        } else if (com.equals(HOMELIST)) {
            IInformationCtrl info;
            if(bundle.getInt("type_id")==0){
                 info = gSocket.GetInfoList(bundle.getInt("status"),null , bundle.getInt("start"),  Config.PAGE);// bundle.getInt("gi_id")
            }else {

                 info = gSocket.GetInfoList(bundle.getInt("status"), bundle.getInt("type_id"), bundle.getInt("start"), Config.PAGE);// bundle.getInt("gi_id")
            }
//            if (info != null) {
                handler.sendMessage(handler.obtainMessage(HOMELISTERROR, info));
//            }
            //收藏
        } else if (com.equals(SHOUCANG)) {
            info = gSocket.InfoFocus(bundle.getInt("infoid"), bundle.getInt("loginid"), bundle.getInt("praiseType"));
            handler.sendMessage(handler.obtainMessage(SHOUCANGERROR, info));
            //点赞
        } else if (com.equals(LICK)) {
            info = gSocket.InfoPraise(bundle.getInt("infoid"), bundle.getInt("loginid"), bundle.getInt("praiseType"));
            handler.sendMessage(handler.obtainMessage(LICKERROR, info));
            //搜索
        } else if (com.equals(SEARCH)) {
            IInformationCtrl info = gSocket.GetInfoListBySearch(bundle.getString("content"), bundle.getInt("start"), bundle.getInt("pagesize"));//,bundle.getInt("gi_id")
            handler.sendMessage(handler.obtainMessage(SEARCHERROR, info));

            //获取地址
        } else if (com.equals(ADDRESS)) {
            IGeoEntityCtrl info = gSocket.GetGeoinfoP(bundle.getInt("gi_id"), bundle.getInt("prev_id"), bundle.getString("type"));
            handler.sendMessage(handler.obtainMessage(ADDRESSERROR, info));
            //获取地址1
        } else if (com.equals(ADDRESS1)) {
            IGeoinfoCtrl ctrl;
            if (bundle.getInt("i") < 0)
                ctrl = gSocket.GetGeoinfo(0, 0, "120100000000", "");
            else {
                ctrl = gSocket.GetGeoinfo(0, bundle.getInt("i"), "", "");
            }
            handler.sendMessage(handler.obtainMessage(ADDRESSERROR1, ctrl));
            //获取天气
        } else if (com.equals(WEATHER)) {
            IWeatherCtrl info = gSocket.GetWeatherInfo();
            handler.sendMessage(handler.obtainMessage(WEATHERERROR1, info));
            //获取商家状态
        }else if(MERCHANT.equals(com)){
            BusinessEntity info=(BusinessEntity)gSocket.GetBusInfo(gSocket.login_id);
            handler.sendMessage(handler.obtainMessage(MERCHANTERROR, info));
            //积分中心获取总积分
        }else if(JIFENOTAL.equals(com)){
            info =(IpointsCtrl)gSocket.getTotalPoints(gSocket.login_id);
            handler.sendMessage(handler.obtainMessage(JIFENOTALERROR, info));
            //积分中心的明细
        }else if(JIFENMINGXI.equals(com)){
            info =(IpointsCtrl)gSocket.getPointsList(gSocket.login_id,bundle.getInt("start"),10);
            handler.sendMessage(handler.obtainMessage(JIFENMINGXIERROR, info));
            //积分中心可兑换列表
        }else if(ARROWLIST.equals(com)){
            info =(IExChangeCtrl)gSocket.getProductList(bundle.getInt("start"),bundle.getInt("pagesize"));
            handler.sendMessage(handler.obtainMessage(ARROWLISTERROR, info));
            //积分中心的可兑换详情
        }else if(MECHANTDETAIL.equals(com)){
            info =(ExProductEntity)gSocket.getProductInfo(bundle.getInt("exp_id"));
            handler.sendMessage(handler.obtainMessage(MECHANTDETAILERROR, info));
            //积分兑换
        }else if(DUIHUAN.equals(com)){
            info =(ICtrl)gSocket.exChange(gSocket.login_id,bundle.getInt("exp_id"),bundle.getInt("type"));
            handler.sendMessage(handler.obtainMessage(DUIHUANERROR, info));
            //我的代金券列表
        }else if(DAIJINJUANLIST.equals(com)){
            info =(IExChangeCtrl)gSocket.getProListByLogin(gSocket.login_id,bundle.getInt("status"),bundle.getInt("start"),10);
            handler.sendMessage(handler.obtainMessage(DUIHUANERROR, info));
        }else if(DAIJINDETAIL.equals(com)){
            info =(VoucherEntity)gSocket.getVoucherInfo(bundle.getInt("voucher_id"));
            handler.sendMessage(handler.obtainMessage(DAIJINDETAILERROR, info));

        }else if(SEARCHMECHANT.equals(com)){
            info =(IInformationCtrl)gSocket.getBusInfoListBySearch(bundle.getString("key"),bundle.getInt("start"),Config.PAGE);
            handler.sendMessage(handler.obtainMessage(SEARCHERROR, info));
        }else if(SETLOG.equals(com)){
            Integer mainid;
            if(bundle.getInt("type")== ClickTimesType.weather.toValue()){
                mainid=null;
            }else{
                mainid=bundle.getInt("type");
            }
                    if( gSocket.login_id==0){
                       gSocket.addClickTimes(null, bundle.getInt("type"), mainid, bundle.getString("serialUmber"));
                    }else{
                        gSocket.addClickTimes(gSocket.login_id,bundle.getInt("type"),mainid,null);
                    }
            //支付宝支付
        }else if(ALPAY.equals(com)){
            IAplipay pars = gSocket.getAplipayOrderInfo(bundle.getString("ori"), bundle.getDouble("amout"),0);
            if (pars!=null)
                handler.sendMessage(handler.obtainMessage(ALPAYERROR, pars));
        }
    }

    /**
     * 发布信息接口
     * @param obj
     */
    public void sendRelease(InfomationEntity obj){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, RELEASE);
        this.bundle.putSerializable("obj",obj);
        this.Run();

    }

    /**
     * 首页面获取信息列表
     *
     status 状态  0 草稿 1发布 2待审核 3违规退回 ，默认传null
     type_id 信息分类，见枚举  可传null  InfoType
     gi_id   当前小区id
     start
     pagesize每页行数
     */
    public void getMessageList(int state,int messageType,int currentArea,int startPage){

        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, HOMELIST);
        this.bundle.putInt("status",state);
        this.bundle.putInt("type_id",messageType);
        this.bundle.putInt("gi_id",currentArea);
        this.bundle.putInt("start",startPage);
        this.Run();
    }

    /**
     * 收藏
     */
    public void collect(int infoid,int loginid,int praiseType){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, SHOUCANG);
        this.bundle.putInt("infoid",infoid);
        this.bundle.putInt("loginid",loginid);
        this.bundle.putInt("praiseType",praiseType);
        this.Run();
    }

    /**
     * 点赞
     */
    public void like(int infoid,int loginid,int praiseType){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, LICK);
        this.bundle.putInt("infoid",infoid);
        this.bundle.putInt("loginid",loginid);
        this.bundle.putInt("praiseType",praiseType);
        this.Run();
    }

    /**
     * 搜索
     * @param content
     * @param start
     * @param pagesize
     */
    public void search(String content,int gi_id, int start,int pagesize){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, SEARCH);
        this.bundle.putString("content",content);
        this. bundle.putInt("gi_id",gi_id);
        this.bundle.putInt("start",start);
        this.bundle.putInt("pagesize",pagesize);
        this.Run();

    }

    /**
     * 获取地址
     * @param gi_id
     * @param prev_id
     * @param type
     */
    public  void getAddress(int gi_id, int prev_id,   String type){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, ADDRESS);
        this.bundle.putInt("gi_id",gi_id);
        this.bundle.putInt("prev_id",prev_id);
        this.bundle.putString("type",type);
        this.Run();

    }
    /**
     * 李正地址
     *
     */

    public void getAddress1(int i){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, ADDRESS1);
        this.bundle.putInt("i",i);
        this.Run();

    }
    //获取天气接口
    public void getWeather(){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, WEATHER);
        this.Run();
    }

    /**
     * 是否为商家
     */
    public void getMerchant(){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, MERCHANT);
        this.Run();
    }

    /**
     * 积分中心
     *
     * 获取总积分
     */
    public void getTotalJifen(){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, JIFENOTAL);
        this.Run();
    }

    /**
     * 积分中心
     *
     * 积分明细
     */
    public void getJifenMingxi(int start){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, JIFENMINGXI);
        this.bundle.putInt("start",start);
        this.bundle.putInt("pagesize", Config.PAGE);
        this.Run();
    }

    /**
     * 积分中心
     *
     * 可兑换产品列表
     */
    public void getArrowList(int start){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, ARROWLIST);
        this.bundle.putInt("start",start);
        this.bundle.putInt("pagesize", Config.PAGE);
        this.Run();
    }

    /**
     * 积分中心
     * 可兑换商品详情
     * @param exp_id
     */
    public void getMechantDetail(int exp_id){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, MECHANTDETAIL);
        this.bundle.putInt("exp_id",exp_id);
        this.Run();
    }


    /**
     * 积分中心
     *
     * 积分兑换
     */
    public void duiHuan(int exp_id,int type){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, DUIHUAN);
        this.bundle.putInt("exp_id",exp_id);
        this.bundle.putInt("type",type);
        this.Run();
    }

    /**
     * 礼券中心
     *
     * 代金券列表
     */
    public void getDiajinList(Integer status,int start){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, DAIJINJUANLIST);
        this.bundle.putInt("status",status);
        this.bundle.putInt("start",start);
        this.Run();
    }

    /**
     * 代金券详情
     * @param voucher_id
     */
    public void getDJDetial(int voucher_id){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, DAIJINDETAIL);
        this.bundle.putInt("voucher_id",voucher_id);
        this.Run();
    }

    /**
     * 搜索 根据商家名称搜索
     */
    public void setSearchChant(String key,int start){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, SEARCHMECHANT);
        this.bundle.putString("key",key);
        this.bundle.putInt("start",start);
        this.Run();
    }

    /**
     * 向服务器发送log
     * @param type
     * @param mainid
     * @param serialUmber
     */
    public void setLog(int type,Integer mainid,String serialUmber){
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, SETLOG);
        this.bundle.putInt("type",type);
        this.bundle.putInt("mainid",mainid);
        this.bundle.putString("serialUmber",serialUmber);
        this.Run();

    }

    /**
     * 支付宝支付接口
     * @param ori
     * @param amout
     */
    public void getPayDemo(String ori, double amout) {
        this.bundle = new Bundle();
        this.bundle.putString(CMD_CODE, ALPAY);
        this.bundle.putString("ori", ori);
        this.bundle.putDouble("amout", amout);
        this.Run();
    }


}
